package com.itservice.uzalarm;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.Window;

public class meiro3 extends Activity
{
	private static boolean isExit = false;
    private SurfaceView mSvMain;
    private MazeProc mMainDrawArea;
	private UzalarmManager uzalarmManager;
    private static meiro3 mMaze;
    public	static UzalarmMediaPlayer	mMedia	= null;
	
	// ステータスバー
	public	static	NotificationManager mManager = null;
	public	static	Notification n = null;

	public Intent intent = null;
	public	String mFilePath = null;
	TelephonyManager telMan;

	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        isExit = false;

		// ステータスバー
    	mManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    	n = new Notification();

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        intent = getIntent();
		mFilePath = intent.getStringExtra("uzalarm_music_path");

		mMaze = this;

        setContentView(R.layout.wakeup);
        // SurfaceViewを参照
        mSvMain = (SurfaceView)findViewById(R.id.SurfaceViewMain);

		// 作成したMainSurfaceViewクラスをインスタンス化
        mMainDrawArea = new MazeProc(this, mSvMain);

		mMedia = new UzalarmMediaPlayer(meiro3.this, mFilePath);
		mMedia.AlarmStart();

		telMan = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		telMan.listen(
				mListener,
				PhoneStateListener.LISTEN_CALL_STATE
		);
		
    }

    //------------------------------------------------------
	// OnResume 迷路作成
	//------------------------------------------------------
	@Override
	public void onResume()
	{
		super.onResume();
		mManager.cancel(1);
	}

	@Override
	public void onPause()
	{
		super.onPause();

		if(false ==isExit)
		{
			n.icon = R.drawable.icon;
			n.when = System.currentTimeMillis();
	
			Intent i = new Intent(getApplicationContext(),meiro3.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			PendingIntent pend = PendingIntent.getActivity(this, 0, i, 0);
	
			n.setLatestEventInfo(getApplicationContext(), "Alarm実行中", "クリックで迷路が立ち上がります",pend );
			mManager.notify(1,n);
		}
		else
		{
			if(mMedia != null)
			{
				//UzalarmManagerのインスタンス取得
	        	uzalarmManager = UzalarmManager.getInstance();
	        	//キー再ロック
	        	uzalarmManager.Keyguard_Lock(this);

	        	mMedia.AlarmStop();
				mMedia = null;
			}
		}
	}

	public void onDestory()
	{
		super.onDestroy();
		isExit = false;
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
        {
        	// バックキーの無効化
            switch (event.getKeyCode())
            {
                case KeyEvent.KEYCODE_BACK:
                    return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		boolean flag = false;

		flag = mMainDrawArea.onTouchEvent(event);

		if(false == flag) 
		{
			//終了する。
			exit();
		}

		return true; 
	}

	public PhoneStateListener mListener = new PhoneStateListener()
	{
		@Override
		public void onCallStateChanged(int state, String incomingNumber)
		{
			super.onCallStateChanged(state, incomingNumber);

			if(null == mMedia)
        	{
        		return;
        	}

        	switch(state)
            {
                case TelephonyManager.CALL_STATE_RINGING:
                case TelephonyManager.CALL_STATE_OFFHOOK:
                	mMedia.AlarmStop();
                	break;

                case TelephonyManager.CALL_STATE_IDLE:
                	mMedia.AlarmStart();
        			break;
             };
		}
	};

	//終了
    public void exit()
    {
    	isExit = true;
    	mMaze.finish();
    	
    	//onPauseより先に呼ばれる可能性があるので、nullでなければ音楽を停止する。
		if(null != mMedia)
    	{
			//UzalarmManagerのインスタンス取得
        	uzalarmManager = UzalarmManager.getInstance();
        	//キー再ロック
        	uzalarmManager.Keyguard_Lock(this);

        	mMedia.AlarmStop();
			mMedia = null;
    	}
    }
}
